//
//  PEDeviceStatusCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/10/12.
//

#import "RHDeviceStatusCell.h"
#import "RHDeviceStatusItemCell.h"

@interface RHDeviceStatusCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate>

@property (nonatomic, strong) UIPageControl *pageCtl;

@property (nonatomic, strong) NSMutableArray *statusViewArr;

@property (nonatomic, strong) UICollectionView *collectionView;
@end

@implementation RHDeviceStatusCell
-(NSMutableArray *)statusViewArr
{
    if(!_statusViewArr){
        _statusViewArr = [NSMutableArray array];
    }
    return _statusViewArr;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
    }
    return self;
}

-(void)hidePageControl
{
    self.pageCtl.hidden = YES;
}

-(void)setDataArr:(NSArray *)dataArr
{
    _dataArr = dataArr;
    self.pageCtl.numberOfPages = dataArr.count;
    self.pageCtl.hidden = dataArr.count<2;
    [self.collectionView reloadData];
}


-(void)setupUI
{
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    
    UIView *bgView = kView;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    bgView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
        make.height.offset(kUI_WINDOW_W*0.5);
    }];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection =  UICollectionViewScrollDirectionHorizontal;
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    [_collectionView registerClass:[RHDeviceStatusItemCell class] forCellWithReuseIdentifier:@"PEDeviceStatusItemCell"];
    _collectionView.backgroundColor = [UIColor clearColor];
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = YES;
    [bgView addSubview:_collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    UIPageControl *pageCtl = [[UIPageControl alloc]init];
    pageCtl.numberOfPages = 4;
    pageCtl.currentPage = 0;
    pageCtl.pageIndicatorTintColor = kCOLOR_HEX(@"DCDCDC");
    pageCtl.currentPageIndicatorTintColor = kCOLOR_HEX(@"56C271");
    [bgView addSubview:pageCtl];
    self.pageCtl = pageCtl;
    [pageCtl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(-5);
        make.centerX.mas_offset(0);
    }];
    [pageCtl addTarget:self action:@selector(pageDidChage:) forControlEvents:UIControlEventValueChanged];
    
    
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if(self.dataArr.count == 0)return 1;
    return self.dataArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RHDeviceStatusItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PEDeviceStatusItemCell" forIndexPath:indexPath];
    if (self.dataArr.count > 0) {
        cell.dic = self.dataArr[indexPath.row];
    }
    
    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(kUI_WINDOW_W , kUI_WINDOW_W*0.5);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    NSLog(@"----->1 %d",scrollView.isDragging);
//    NSLog(@"----->2 %d",scrollView.isTracking);
//    NSLog(@"----->3 %d",scrollView.isDecelerating);
    if(scrollView.isDragging){
        CGFloat pageWidth = kUI_WINDOW_W - 30;
        NSInteger index;
        CGFloat x = scrollView.contentOffset.x + pageWidth*0.5;
        index = x /pageWidth;
        
        self.pageCtl.currentPage = index;
//        NSLog(@"---->index:%zd",index);
    }
    
    
}

-(void)pageDidChage:(UIPageControl*)pageCtl
{
    CGFloat pageWidth = kUI_WINDOW_W ;
    [self.collectionView setContentOffset:CGPointMake(pageWidth * pageCtl.currentPage, 0) animated:YES];
    
    NSLog(@"---->index:%zd",pageCtl.currentPage);
}

@end
